//
//  StoreInfoEditController.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/21.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "StoreInfoEditController.h"
#import "JFBStoreInfoDS.h"

@interface StoreInfoEditController () {
    JFBStoreInfoDS* _storeInfo;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet UITextField *masterNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *storeNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *storeMobileTextField;
@property (weak, nonatomic) IBOutlet UITextField *storeAddressTextField;
@property (weak, nonatomic) IBOutlet UITextField *workTimeTextField;
@property (weak, nonatomic) IBOutlet UITextField *refundTimeTextField1;
@property (weak, nonatomic) IBOutlet UITextField *refundTimeTextField2;
@property (weak, nonatomic) IBOutlet UIButton *addRefundTimeBtn;
@property (weak, nonatomic) IBOutlet UITextField *ccMobileTextField1;
@property (weak, nonatomic) IBOutlet UITextField *ccMobileTextField2;
@property (weak, nonatomic) IBOutlet UITextField *beginPriceTextField;
@property (weak, nonatomic) IBOutlet UISwitch *swBtn;
@property (weak, nonatomic) IBOutlet UITextField *distributionTextField;
- (IBAction)saveAction:(id)sender;
- (IBAction)addRefundTimeAction:(id)sender;

@end

@implementation StoreInfoEditController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.saveBtn setBackgroundColor:TextColorImportButton];
    
    [self reload:nil];
}

- (void)reload:(__unused id)sender {
    
    //    NSURLSessionTask *task =
    [JFBStoreInfoDS loadStoreInfo:^(JFBStoreInfoDS *storeInfo, NSError *error) {
        
        _storeInfo = storeInfo;
        
        self.storeNameTextField.text = storeInfo.storeName;
        self.masterNameTextField.text = storeInfo.masterName;
        self.storeMobileTextField.text = storeInfo.masterMobile;
        self.storeAddressTextField.text = storeInfo.storeAddress;
        
        self.workTimeTextField.text = storeInfo.workTime;
        self.refundTimeTextField1.text = [storeInfo.timeRange objectAtIndex:0];
//        self.refundTimeTextField2.text = [storeInfo.timeRange objectAtIndex:1];
        
        self.ccMobileTextField1.text = [storeInfo.callcenterMobile objectAtIndex:0];
//        self.ccMobileTextField2.text = [storeInfo.callcenterMobile objectAtIndex:1];
        
        self.beginPriceTextField.text = [NSString stringWithFormat:@"%lu", (unsigned long)storeInfo.min];
        self.distributionTextField.text = [NSString stringWithFormat:@"%lu", (unsigned long)storeInfo.distribution];
        
        [self.swBtn setSelected:storeInfo.isStoreOpen];
        
    } storeID:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addRefundTimeAction:(id)sender {
}
- (IBAction)saveAction:(id)sender {
    _storeInfo.storeName = self.storeNameTextField.text;
    _storeInfo.storeAddress = self.storeAddressTextField.text;
    _storeInfo.masterName = self.masterNameTextField.text;
    _storeInfo.masterMobile = self.storeMobileTextField.text;
    
    _storeInfo.workTime = self.workTimeTextField.text;
    _storeInfo.timeRange = @[self.refundTimeTextField1.text, self.refundTimeTextField2.text];
    _storeInfo.callcenterMobile = @[self.ccMobileTextField1.text, self.ccMobileTextField2.text];
    
    _storeInfo.min = [NSString formatDecimal:self.beginPriceTextField.text]; //[self.beginPriceTextField.text intValue];
    _storeInfo.distribution = [self.distributionTextField.text intValue];
    
    _storeInfo.isStoreOpen = self.swBtn.selected;
    
    [JFBStoreInfoDS updateStoreInfo:^(BOOL isSuccess, NSString *message, JFBStoreInfoDS *storeInfo, NSError *error) {
        [JFBUIAnimation alertTips:message];
        [self.navigationController popViewControllerAnimated:YES];
    } storeInfo:_storeInfo];
}
@end
